package com.twu.biblioteca;

public class Movie implements LibraryItem {
    private final String code;
    private final String title;
    private final int year;
    private final String director;
    private final int rating;

    public Movie(String code, String title, int year, String director, int rating) {
        this.code = code;
        this.title = title;
        this.year = year;
        this.director = director;
        this.rating = rating;
    }

    public String getCode() {
        return code;
    }

    @Override
    public String toString() {
        return String.format("%s. %s (%s) By %s (%s)", code, title, year, director, getRatingString());
    }

    @Override
    public boolean equals(Object other) {
        return other instanceof Movie && this.toString().equals(other.toString());
    }

    @Override
    public int hashCode() {
        int result = code.hashCode();
        result = 31 * result + title.hashCode();
        result = 31 * result + (director != null ? director.hashCode() : 0);
        return result;
    }

    private String getRatingString() {
        if (rating == 0) {
            return "unrated";
        }

        return String.format("%s/10", rating);
    }
}
